class MoviesController < ApplicationController
  
  # GET /movies
  # GET /movies.xml
  # GET /movies.json
  def index
    start = (params[:start] || 1).to_i      
    size = (params[:limit] || 20).to_i 
    sort_col = (params[:sort] || 'id')
    sort_dir = (params[:dir] || 'ASC')
    
    page = ((start/size).to_i)+1
    
    @movie_pages = Paginator.new(self, Movie.count, size, page)
    
    @movies = Movie.find(:all, 
                         :select => "id, title, plot, date, genre, mpaa, directed_by", 
    :limit=>@movie_pages.items_per_page,
    :offset=>@movie_pages.current.offset, 
    :order=>sort_col+' '+sort_dir)
    
    return_data = Hash.new()      
    return_data[:totalCount] = @movie_pages.item_count      
    return_data[:results] = @movies.collect{|u| {:id=>u.id, 
        :title=>u.title,
        :plot=>u.plot, 
        :date=>u.date, 
        :genre=>u.genre, 
        :mpaa=>u.mpaa, 
        :directed_by=>u.directed_by} }      
    
    respond_to do |format|
      format.html # index.rhtml
      #      format.xml  { render :xml => @rest_models.to_xml }
      format.json  { render :text=>return_data.to_json, :layout=>false }
    end
  end
end
